package com.erp.framework.controller.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.rmi.RemoteException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.ProjectPrintFormConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.common.web.CustomPdfTable;
import com.erp.framework.SimpleConstants;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.schema.TableSchema;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public abstract class SimpleBasePrintFormPdfAction implements Serializable, SimpleConstants{

	public String companyCode;
	public String status;
	public String printAction;
	public byte[] companyLogoImage;
	public boolean printCompanyLogo;
	public boolean printHeader;
	public boolean printFooter;
	public SelectDelegate sdg;
	public Rectangle docPageSize;
    public static final Font FONT_DEFAULT_HEADER_FOOTER = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 7, Font.NORMAL);
    public static final Font FONT_DEFAULT_HEADER_FONT = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 7, Font.NORMAL);
    public static final Font FONT_DEFAULT_TABLE_CELL_XSMALL_FONT_NORMAL = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 5, Font.NORMAL);
    public static final Font FONT_DEFAULT_TABLE_CELL_XSMALL_FONT_BOLD = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 5, Font.BOLD);
    public static final Font FONT_DEFAULT_TABLE_CELL_SMALL_FONT_NORMAL = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 7, Font.NORMAL);
    public static final Font FONT_DEFAULT_TABLE_CELL_SMALL_FONT_BOLD = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 7, Font.BOLD);
    public static final Font FONT_DEFAULT_TABLE_CELL_FONT_NORMAL = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 9, Font.NORMAL);
    public static final Font FONT_DEFAULT_TABLE_CELL_FONT_BOLD = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 9, Font.BOLD);
    public static final Font FONT_DEFAULT_TABLE_HEADING_FONT_NORMAL = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 11, Font.NORMAL);
    public static final Font FONT_DEFAULT_TABLE_HEADING_FONT_BOLD = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 11, Font.BOLD);
    public static final Font FONT_DEFAULT_TITLE_FONT_NORMAL = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 16, Font.NORMAL);
    // the font for pdf print
    public static final Font FONT_DEFAULT_TITLE_FONT_BOLD = FontFactory.getFont(ProjectPrintFormConstants.FONT_DEFAULT, 16, Font.BOLD);
	
	static Font defaultTitleBoldFont = SimpleBasePrintFormPdfAction.FONT_DEFAULT_TITLE_FONT_BOLD;
	static Font defaultHeaderBoldFont = SimpleBasePrintFormPdfAction.FONT_DEFAULT_TABLE_HEADING_FONT_BOLD;
	static Font defaultContentBoldFont = SimpleBasePrintFormPdfAction.FONT_DEFAULT_TABLE_CELL_FONT_BOLD;
	static Font defaultContentNormalFont = SimpleBasePrintFormPdfAction.FONT_DEFAULT_TABLE_CELL_FONT_NORMAL;
	public static String BORDER_TOP = "border_top";
	public static String BORDER_BOTTOM = "border_bottom";
	public static String BORDER_LEFT = "border_left";
	public static String BORDER_RIGHT = "border_right";
	public static Phrase PHRASE_EMPTY = new Phrase("");
	
	// DEFAULT_CELL_HEIGHT here is 0 
	// The PdfPCell.height() method will always return 0 if you didn't set its FixedHeight
	public static float DEFAULT_CELL_HEIGHT = new PdfPCell(PHRASE_EMPTY).height();
	
	public SimpleBasePrintFormPdfAction(){
		printCompanyLogo = true;
		printHeader = true;
		printFooter = true;
		docPageSize = new Rectangle(ProjectConstants.PDF_DOTMX_PAGE_SIZE_LENGTH,
				ProjectConstants.PDF_DOTMX_PAGE_SIZE_WIDTH);
	}	
	   
    //can be override in child classes if needed
    protected Rectangle getPrintFormDefaultRectangle(){
    	return PageSize.A4;
    }

    //can be override in child classes if needed
    protected void setPrintFormDefaultMargins(Document document){
//    	For Page Margin, 1inch = 72pt, 1inch = 2.54cm
        final int PDF_LASER_PAGE_LEFT_MARGIN = 18;              //unit in pt, = 0.25 inch
        final int PDF_LASER_PAGE_RIGHT_MARGIN  = 18;            //unit in pt, = 0.25 inch
        final int PDF_LASER_PAGE_TOP_MARGIN = 54;               //unit in pt, = 0.75 inch
        final int PDF_LASER_PAGE_BOTTOM_MARGIN = 54;            //unit in pt, = 0.75 inch    
        final int PDF_DOTMX_PAGE_LEFT_MARGIN = 18;              //unit in pt, = 0.25 inch
        final int PDF_DOTMX_PAGE_RIGHT_MARGIN = 18;             //unit in pt, = 0.25 inch   
        final int PDF_DOTMX_PAGE_TOP_MARGIN = 180;              //unit in pt, = 2.5 inch
        final int PDF_DOTMX_PAGE_BOTTOM_MARGIN = 54;            //unit in pt, = 0.75 inch
        
    	document.setMargins(PDF_LASER_PAGE_LEFT_MARGIN, PDF_LASER_PAGE_RIGHT_MARGIN, PDF_LASER_PAGE_TOP_MARGIN, PDF_LASER_PAGE_BOTTOM_MARGIN);
    }
    
    //can be override in child classes if needed
    protected Document getPrintFormDefaultDocument(){
    	
    	Document document = new Document(getPrintFormDefaultRectangle());
    	setPrintFormDefaultMargins(document);
    	return document;
    }
    
	protected abstract void setDefaultValues(ActionForm formIn) throws RemoteException, FindRecordException;
	
	protected boolean needBaos() { return false; }
	
	protected abstract Document preparePdfDoc(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response,
			ServletContext sc, PdfWriter writer, Document document);
	
	protected Document preparePdfDoc(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response,
			ServletContext sc, PdfWriter writer, Document document, ByteArrayOutputStream baos){return null;}
    
    protected abstract void setPdfDocPassword(PdfWriter writer, HttpSession session, String docType);
    
	public static void outputByteArrayStream(HttpServletRequest request, HttpServletResponse response, HttpSession session
			, ByteArrayOutputStream baos) 
		throws IOException, DocumentException {

		OutputStream out = response.getOutputStream();
		response.setContentLength(baos.size());
		baos.writeTo(out);

		out.flush();		
		out.close();

		baos = null;
	}
	
	public static PdfPCell getNoBorderBlankCell() {

		return getCharacterCell("", 0, "", null);
	}

	public static PdfPCell getCharacterCell(String borderLocate, float border, String character, Font characterfont) {

		PdfPCell cell = null;
		if (!CommonUtil.isEmpty(character)) {
			cell = new PdfPCell(new Paragraph(character, characterfont));
		} else {
			cell = new PdfPCell(new Paragraph(""));
		}
		cell.setBorder(Rectangle.NO_BORDER);
		if (BORDER_TOP.equals(borderLocate))
			cell.setBorderWidthTop(border);
		else if (BORDER_BOTTOM.equals(borderLocate)) {
			cell.setBorderWidthBottom(border);
		} else if (BORDER_LEFT.equals(borderLocate)) {
			cell.setBorderWidthLeft(border);
		} else if (BORDER_RIGHT.equals(borderLocate)) {
			cell.setBorderWidthRight(border);
		}

		return cell;
	}

	public static PdfPCell getCharacterCellWithTopBorder(float border, String character, Font characterfont) {
		return getCharacterCell(BORDER_TOP, border, character, characterfont);
	}

	public static PdfPTable getOneLineTableWithCharacter(float[] width, int characterCell, String character, Font characterfont) {
		PdfPTable table = null;
		PdfPCell cell = null;
		if (characterCell > width.length - 1) {
			return null;
		}
		table = new PdfPTable(width.length);
		for (int i = 0; i < width.length; i++) {
			if (i == characterCell) {
				cell = new PdfPCell(new Paragraph(character, characterfont));
				cell.setBorder(Rectangle.NO_BORDER);
				table.addCell(cell);
			} else {
				table.addCell(getNoBorderBlankCell());
			}
		}
		return table;
	}

	public static PdfPTable getOneTableWithCharacterFontBold9NoBorder(String character) {
		float[] width = { 1.00f };
		return getOneLineTableWithCharacter(width, 0, character, defaultContentBoldFont);
	}

	public static PdfPTable getOneTableNotWithCharacter() {
		float[] width = { 1.00f };
		return getOneLineTableWithCharacter(width, 0, "", defaultContentBoldFont);
	}

	public static PdfPTable getOneTableWithOneCell(PdfPCell cell) {
		PdfPTable table = null;
		float[] width = { 1.00f };
		table = new PdfPTable(width.length);
		table.addCell(cell);
		return table;
	}
	
	public static PdfPTable createDefaultPTable(float[] widths) throws DocumentException{	            
	    return createLargePTable(widths);
	}
	
	public static PdfPTable createSmallPTable(float[] widths) throws DocumentException{	            
	    return createPTable(widths, 25.0f);
	}
	
	public static PdfPTable createMediumPTable(float[] widths) throws DocumentException{	            
	    return createPTable(widths, 50.0f);
	}
	
	public static PdfPTable createLargePTable(float[] widths) throws DocumentException{	            
	    return createPTable(widths, 100.0f);
	}
	
	public static PdfPTable createPTable(float[] widths, float widthPercent) throws DocumentException{	            
	    PdfPTable table = new PdfPTable(widths.length);
	    table.setWidths(widths);
	    table.setWidthPercentage(widthPercent);	    
	    return table;
	}	
    
	public static void addEmptyCell(PdfPTable table) {
//	    table.addCell(new PdfPCell(new Phrase("")));
		PdfPCell cell = new PdfPCell(new Phrase(""));
		cell.setBorder(Rectangle.NO_BORDER);
		
		table.addCell(cell);
	}
	
	public static void addEmptyCell(PdfPTable table,int colspan,int border,float height){
		PdfPCell cell = new PdfPCell(PHRASE_EMPTY);
		cell.setColspan(colspan);
		cell.setBorder(border);
		if(height != DEFAULT_CELL_HEIGHT){
			cell.setFixedHeight(height); 
		}   
		table.addCell(cell);
	}
	 
	public static void addNEmptyCell(PdfPTable table, int no) {
		for(int i = 0; i < no; i++){
			addEmptyCell(table);
		}
	}	
	 
	public static void addBoxEmptyCell(PdfPTable table) {
		PdfPCell cell = new PdfPCell(new Phrase(""));
		cell.setBorder(Rectangle.BOX);
		
		table.addCell(cell);
	}
	
	public static void addBoxNEmptyCell(PdfPTable table, int no) {
		for(int i = 0; i < no; i++){
			addBoxEmptyCell(table);
		}
	}		
    
    public static void addDefaultBoxCell(PdfPTable table, Object obj) {
    	addMediumBoxCell(table, obj);
	}
    
    public static void addDefaultBoxCellBold(PdfPTable table, Object obj) {
    	addMediumBoxCellBold(table, obj);
	}
	
	public static void addDefaultCell(PdfPTable table, Object obj) {
		addMediumCell(table, obj);
	}
	
	public static void addDefaultCellBold(PdfPTable table, Object obj) {
		addMediumCellBold(table, obj);
	}
		
	public static void addSmallDtlHdrCellLabel(PdfPTable table, TableSchema ts, String labelId) {
		String cellStr = ts.getDisplayWebLabelByObjectId(labelId);
		addSmallDtlHdrCellNormal(table, cellStr);
	}

	public static void addSmallDtlHdrCellNormal(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 7, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,
				Rectangle.NO_BORDER);
	}

	public static void addSmallDtlHdrCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 7, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM,
				Rectangle.NO_BORDER);
	}

	public static void addSmallCellBoldUnderLine(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD | Font.UNDERLINE, 7, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}

	public static void addMediumCellBoldUnderLine(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD | Font.UNDERLINE, 9, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}

	public static void addCell(PdfPTable table, Object obj, String fontName, int fontStyle, int fontSize, int alignH, int alignV,
			int border) {
		String cellStr = CommonUtil.nullToString(obj);
		PdfPCell cell = new PdfPCell(new Phrase(cellStr, FontFactory.getFont(fontName, fontSize, fontStyle)));

		cell.setBorder(border);
		cell.setHorizontalAlignment(alignH);
		cell.setVerticalAlignment(alignV);
		table.addCell(cell);
	}
		
	public static void addXSmallCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addXSmallCellBold(table, cellStr);
	}
	
	public static void addXSmallBoxCellLabel(PdfPTable table, TableSchema ts, String labelId, boolean boldFace){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);   
		if(boldFace){
			addXSmallBoxCellBold(table, cellStr);
		} else {
			addXSmallBoxCell(table, cellStr);
		}
	}
	
	public static void addSmallCellLabel(PdfPTable table, TableSchema ts, String labelId, int border){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addSmallCellBold(table, cellStr, border);
	}
	
	public static void addSmallCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addSmallCellBold(table, cellStr);
	}
	
	public static void addSmallBoxCellLabel(PdfPTable table, TableSchema ts, String labelId, boolean boldFace){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);   
		if(boldFace){
			addSmallBoxCellBold(table, cellStr);
		} else {
			addSmallBoxCell(table, cellStr);
		}
	}
	
	public static void addMediumCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addMediumCellBold(table, cellStr);
	}
	
	public static void addMediumBoxCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addMediumBoxCellBold(table, cellStr);
	}
	
	public static void addLargeCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addLargeCellBold(table, cellStr);
	}
	
	public static void addLargeBoxCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addLargeBoxCellBold(table, cellStr);
	}
	
	public static void addHeadingCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addHeadingCellBold(table, cellStr);
	}
	
	public static void addHeadingBoxCellLabel(PdfPTable table, TableSchema ts, String labelId){
		String cellStr = ts.getDisplayWebLabelWithColonByObjectId(labelId);          
		addHeadingBoxCellBold(table, cellStr);
	}
    
	public static void addXSmallBoxCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 5, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addXSmallBoxCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 5, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addXSmallCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 5, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addXSmallCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 5, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addSmallBoxCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 7, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addSmallBoxCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 7, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addSmallCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 7, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addSmallCell(PdfPTable table, Object obj, int border) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 7, Element.ALIGN_LEFT, border);
	}

	public static void addSmallCellBold(PdfPTable table, Object obj, int border) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 7, Element.ALIGN_LEFT, border);
	}
	
	public static void addSmallCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 7, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addMediumBoxCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 9, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addMediumBoxCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 9, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addMediumCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 9, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addMediumCell(PdfPTable table, Object obj, int border) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 9, Element.ALIGN_LEFT, border);
	}
	
	public static void addMediumCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 9, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addMediumCellBold(PdfPTable table, Object obj, int border) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 9, Element.ALIGN_LEFT, border);
	}
	
	public static void addLargeBoxCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 11, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addLargeBoxCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 11, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addLargeCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 11, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addLargeCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 11, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addHeadingBoxCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 16, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addHeadingBoxCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 16, Element.ALIGN_LEFT, Rectangle.BOX);
	}
	
	public static void addHeadingCell(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 16, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addHeadingCellBold(PdfPTable table, Object obj) {
		addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 16, Element.ALIGN_LEFT, Rectangle.NO_BORDER);
	}
	
	public static void addCell(PdfPTable table, Object obj, String fontName, int fontStyle, int fontSize, int align, int border) {
		String cellStr = CommonUtil.nullToString(obj);
		PdfPCell cell = new PdfPCell(new Phrase(cellStr, FontFactory.getFont(fontName, fontSize, fontStyle)));
		
		cell.setBorder(border);
		cell.setHorizontalAlignment(align);
		table.addCell(cell);
	}
	
	public static void addImageToCell(PdfPTable table, byte[] byteImage) throws Exception {
		addImageToCell(table, byteImage, 80.0f, Element.ALIGN_CENTER, Rectangle.NO_BORDER);
	}
	
	public static void addImageToCellWithBorder(PdfPTable table, byte[] byteImage) throws Exception {
		addImageToCell(table, byteImage, 80.0f, Element.ALIGN_CENTER, Rectangle.BOX);
	}
	
	public static void addImageToCell(PdfPTable table, byte[] byteImage, float height, int horizAlign, int border) throws Exception {
		PdfPCell cell = null;
		Image image = null;
		try {
			if (byteImage != null && byteImage.length > 0) {
				image = Image.getInstance(byteImage);
			}

			cell = new PdfPCell(image, true);
			cell.setFixedHeight(height);
			cell.setHorizontalAlignment(horizAlign);
			cell.setBorder(border);

			table.addCell(cell);
		} catch (Exception e) {
			throw e;
		}
	}	

	public void renderCompanyLogo(Document document) throws Exception {
		renderCompanyLogo(document, this.companyCode, this.companyLogoImage);		
	}

	public void renderCompanyLogo(Document document, String companyCode, byte[] byteImage) throws Exception {
		PdfPTable table = null;
		PdfPCell cell = null;
		Image image = null;
		try {
			if (byteImage != null && byteImage.length > 0) {
				image = Image.getInstance(byteImage);

			}

			table = new PdfPTable(1);
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);

			cell = new PdfPCell(image, true);
			cell.setFixedHeight(80.0f);
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setBorder(Rectangle.NO_BORDER);
			table.addCell(cell);
			table.setSpacingBefore(8f);

		} catch (Exception e) {
			Logger.debug(e);

		}
		document.add(table);
	}

	public PdfPTable getTitleTable(String pageTitle, boolean isFromDB, Font titleFont) {

		String title = null;
		SelectDelegate selectDelegate = new SelectDelegate();
		if (isFromDB) {
			title = String.valueOf(selectDelegate.selectOneValue(pageTitle));
		} else {
			title = pageTitle;
		}
		PdfPTable table = null;
		PdfPCell cell = null;
		try {
			table = new PdfPTable(1);
			table.getDefaultCell().setBorder(0);

			cell = new PdfPCell(new Paragraph(title, titleFont));
			cell.setBorder(0);
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
		} catch (Exception e) {
			Logger.debug(e);
		}
		return table;
	}

	public void renderHeaderForA4(Document document, String title, String companyCode, String title2) throws Exception {
		CustomPdfTable table = null;
		PdfPCell cell = null;
		float[] widths = new float[] { 3.0f, 3.0f, 1.4f, 0.1f, 2.0f };

		table = new CustomPdfTable(widths);
		table.setWidths(widths);
		table.setWidthPercentage(95.0f);

		cell = new PdfPCell(new Phrase(title, defaultTitleBoldFont));
		cell.setBorder(Rectangle.NO_BORDER);
		cell.setColspan(5);
		cell.setPaddingBottom(18f);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
		SelectDelegate sdg = new SelectDelegate();

		cell = new PdfPCell(new Phrase(title2, defaultHeaderBoldFont));
		cell.setBorder(Rectangle.NO_BORDER);
		cell.setPaddingBottom(6f);
		cell.setBorderWidthBottom(0.05f);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setColspan(5);
		table.addCell(cell);

		document.add(table);
	}

	public void renderHeader(Document document) throws Exception {
		PdfPTable table = null;
		PdfPCell cell = null;
		float[] widths = new float[] { 95.0f };

		table = new PdfPTable(widths.length);
		table.setWidths(widths);
		table.setWidthPercentage(95.0f);

		cell = new PdfPCell(PHRASE_EMPTY);
		cell.setBorder(Rectangle.NO_BORDER);
		cell.setMinimumHeight(100f);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
		table.addCell(cell);

	}
	
    public static void addSmallDtlHdrAlignRightCellBold(PdfPTable table, Object obj) { 
        addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.BOLD, 7, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM, 
                        Rectangle.NO_BORDER); 
	} 
	public static void addSmallAlignRightCell(PdfPTable table, Object obj) { 
	        addCell(table, obj, ProjectConstants.FONT_DEFAULT, Font.NORMAL, 7, Element.ALIGN_RIGHT, Rectangle.NO_BORDER); 
	} 

}
